#include "MailBox.h"

const std::string MailBox::MBOX_NODE = "mailbox";
const std::string MailBox::MBOX_EMAIL_NODE = "mail";
const std::string MailBox::MBOX_REPLYTO_NODE = "replyto";
const std::string MailBox::MBOX_PROJECT_NAME_NODE = "projectName";
const std::string MailBox::MBOX_SENDER_NAME_NODE = "sender";
const std::string MailBox::MBOX_LOGIN_NODE = "login";
const std::string MailBox::MBOX_PASSWORD_NODE = "password";
const std::string MailBox::MBOX_SERVER_NODE = "server";
const std::string MailBox::MBOX_PORT_NODE = "port";
const std::string MailBox::MBOX_SSL_NODE = "useSSL";
const std::string MailBox::MBOX_DEFAULT_MAIL_CLIENT_NODE = "useDefaultMailClient";

const int MailBox::MAIL_SENT = 0;
const int MailBox::MAIL_ERROR = 1;

using namespace rapidxml;
using namespace std;

//bool MailBox::sendMail(Mail* mail){
//	bool sendError = false;
//
//	try
//	{
//		CSmtp sender;
//
//		sender.SetSMTPServer(server->c_str(), port);
//		sender.SetLogin(login->c_str());
//		sender.SetPassword(password->c_str());
//
//		if(useSSL){
//			sender.SetSecurityType(CSmtp::USE_SSL);
//		}
//		else{
//			sender.SetSecurityType(CSmtp::USE_TLS);
//		}
//
//		sender.SetSenderName(senderName->c_str());
//		sender.SetSenderMail(replyTo->c_str());
//		sender.SetReplyTo(replyTo->c_str());
//		sender.SetSubject(mail->getSubject()->c_str());
//		sender.AddRecipient(mail->getTo()->c_str());
//
//		list<string*>::iterator ccIter;
//		for(ccIter = mail->getCC()->begin(); ccIter != mail->getCC()->end(); ccIter++){
//			sender.AddCCRecipient((*ccIter)->c_str());
//		}
//
//		sender.AddMsgLine(mail->getBody()->c_str());
//
//		sender.SetXPriority(XPRIORITY_NORMAL);
//		sender.SetXMailer("CSC Resa");
//
//		sender.Send();
//	}
//	catch(ECSmtp e)
//	{
//		sendError = true;
//	}
//
//
//	return !sendError;
//}

bool MailBox::sendMailWithDefaultClient(Mail* mail){
	stringstream mailtoStr;

	mailtoStr << "mailto:" << mail->getTo()->c_str() << "?subject=" << mail->getSubject()->c_str() << "&cc=";

	list<string*>::iterator ccIter;
	for(ccIter = mail->getCC()->begin(); ccIter != mail->getCC()->end(); ccIter++){
		mailtoStr << (*ccIter)->c_str() << ", ";
	}

	string mailtoBody(mail->getBody()->c_str());
	
	int pos = 0;
	while((pos = mailtoBody.find_first_of('\n')) != string::npos){
		mailtoBody.replace(pos, 1, "%0D");
	}

	mailtoStr << "&replyto=" << replyTo->c_str() << "&body=" << mailtoBody << "%0D";

	return QDesktopServices::openUrl(QUrl(mailtoStr.str().c_str()));
}

bool MailBox::isValid(void) const{
	if(mailAddr == 0 || replyTo == 0 || projectName == 0 /* || senderName == 0 || login == 0 || password == 0 || server == 0*/){
		return false;
	}
	else{
		if(mailAddr->empty() || replyTo->empty()  || projectName->empty() /* || senderName->empty()  || login->empty()  || password->empty()  || server->empty()*/){
			return false;
		}
		else{
			return true;
		}
	}
}

rapidxml::xml_node<>* MailBox::constructNode(rapidxml::xml_document<>* doc) const{

	xml_node<>* mBoxNode = doc->allocate_node(node_element, MailBox::MBOX_NODE.c_str());

	xml_node<>* emailNode = doc->allocate_node(node_element, MailBox::MBOX_EMAIL_NODE.c_str(), getMailAddr()->c_str());
	xml_node<>* replyToNode = doc->allocate_node(node_element, MailBox::MBOX_REPLYTO_NODE.c_str(), getReplyTo()->c_str());
	xml_node<>* projectNameNode = doc->allocate_node(node_element, MailBox::MBOX_PROJECT_NAME_NODE.c_str(), getProjectName()->c_str());
	//xml_node<>* senderNameNode = doc->allocate_node(node_element, MailBox::MBOX_SENDER_NAME_NODE.c_str(), getSenderName()->c_str());
	//xml_node<>* loginNode = doc->allocate_node(node_element, MailBox::MBOX_LOGIN_NODE.c_str(), getLogin()->c_str());
	//xml_node<>* passwordNode = doc->allocate_node(node_element, MailBox::MBOX_PASSWORD_NODE.c_str(), getPassword()->c_str());
	//xml_node<>* serverNode = doc->allocate_node(node_element, MailBox::MBOX_SERVER_NODE.c_str(), getServer()->c_str());


	std::stringstream buffer;
	//buffer << getPort();
	//xml_node<>* portNode = doc->allocate_node(node_element, MailBox::MBOX_PORT_NODE.c_str(), doc->allocate_string(buffer.str().c_str()));

	//buffer.str("");
	//buffer << isSSLenabled();
	//xml_node<>* sslNode = doc->allocate_node(node_element, MailBox::MBOX_SSL_NODE.c_str(), doc->allocate_string(buffer.str().c_str()));

	buffer.str("");
	buffer << shouldUseDefaultMailClient();
	xml_node<>* useDefaultMailClientNode = doc->allocate_node(node_element, MailBox::MBOX_DEFAULT_MAIL_CLIENT_NODE.c_str(), doc->allocate_string(buffer.str().c_str()));

	mBoxNode->append_node(emailNode);
	mBoxNode->append_node(replyToNode);
	mBoxNode->append_node(projectNameNode);
	//mBoxNode->append_node(senderNameNode);
	//mBoxNode->append_node(loginNode);
	//mBoxNode->append_node(passwordNode);
	//mBoxNode->append_node(serverNode);
	//mBoxNode->append_node(portNode);
	//mBoxNode->append_node(sslNode);
	mBoxNode->append_node(useDefaultMailClientNode);

	return mBoxNode;
}